<?php 

# 
# Copyright (c) 2002-2007 Eric Wallengren
# This file is part of the Continuous Automated Build and Integration 
# Environment (CABIE)
# 
# CABIE is distributed under the terms of the GNU General Public
# License version 2 or any later version.  See the file COPYING for copying 
# permission or http://www.gnu.org. 
#                                                                            
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR  
# IMPLIED, without even the implied warranty of MERCHANTABILITY or FITNESS 
# FOR A PARTICULAR PURPOSE.  ANY USE IS AT YOUR OWN RISK. 
#                                                                            
# Permission to modify the code and to distribute modified code is granted, 
# provided the above notices are retained, and a notice that the code was 
# modified is included with the above copyright notice. 
# 

    #
    # This is where the ajax function for job information is called from
    # via the Sajax library
    #

    #
    # import all required php files
    #
    require_once("auth.php");
    require_once("defs.php");
    require_once("unixsys.php");
    require_once("dbdefs.php");
    require_once("common.php");
    require_once('Sajax.php');

    #
    # Pull server and job name from the request header
    #
    $buildserver = $_REQUEST['server'];
    $jobname     = $_REQUEST['jobname'];

    #
    # Outside of SESSION info, this is the only global declaration used
    # for cabie.  
    #
    global $SQL;

    #
    # CReate a new instance of SQL
    #
    $SQL = new sqlfuncs();

    $SQL->__construct($server, $username, $password, $database);

    function ajax_jobinfo($server, $jobname, $recs) {

        $SQL = new sqlfuncs();
        $SQL->__construct('server', 'login', 'password', 'builds');

        $html = _expandJobInfo($SQL, $server, $jobname, $recs);

        return $html;

    }

    function _expandJobInfo($SQL, $buildserver, $jobname, $recs) {

        $states = array("untested", "failed test", "failed build", 
                        "passed test");

        $lights = array("bullet_green.png", "bullet_yellow.png", 
                        "bullet_red.png", "bullet_green.png");

        if (!isset($recs)) {
            $recs = 10;
        }
    
        $jobarray = $SQL->sql_query("select job,port,sccs,status,start,".
                                    "end from jobs where title=\"$jobname\" ".
                                    "order by job desc limit $recs", ";");

        $html  = "<tr>\n".
                 "    <td class=colnames width=100>build id</b></td>\n".
                 "    <td class=colnames width=100>spec</td>\n".
                 "    <td class=colnames width=10 colspan=2>status</td>\n".
                 "    <td class=colnames width=100>tests</td>\n".
                 "    <td class=colnames width=100>when</td>\n".
                 "    <td class=colnames width=100>elapsed</td>\n".
                 "    <td class=colnames width=100>actions</td>\n".
                 "</tr>\n";

        foreach ($jobarray as $job) {

            $html .= "<tr>\n";

            list($jobno, $port, $sccs, $status, $start, $end) = 
                 explode(";", $job);

            $date   = date('m/d/Y h:i:s A', $end);

            $duration  = _formatElapsedTime($end - $start);

            $teststeps = _formatTestStates($SQL, $jobname, $jobno, 
                                           $buildserver);

            $promoted  = _checkPromotion($SQL, $jobname, $jobno);

            $html .= "    <td class=values valign=top align=right><a href=".
                     "/userbroker.php?cmd=".
                     "changed&server=$buildserver&jobname=$jobname&jobno=".
                     "$jobno>$jobno</a></td>\n".
                     "    <td class=values valign=top>$sccs - $port</td>\n";

            if ($status == 2) {
                $comment = "\t<img src=/cabieicons/user_comment.png alt=".
                           "comment onmouseover=\"Tip('add comment for ".
                           "$jobname $jobno')\">";
            } else {
                $comment = "\t<a href=/editor.php?server=$buildserver&name=".
                           "$jobname&jobno=$jobno&action=edit&table=comments>".
                           "<img src=/cabieicons/user_comment.png alt=comment ".
                           "onmouseover=\"Tip('add comment for $jobname ".
                           "$jobno')\"></a>";
            }

            if ($status > 3) {
                $html .= "    <td class=values valign=top><img ".
                         "src=/cabieicons/$lights[1] ".
                         "width=22 height=22></td>\n";
            } else {
                $html .= "    <td class=values valign=top><img ".
                         "src=/cabieicons/$lights[$status] ".
                         "width=22 height=22></td>\n";
            }

            if ($status > 3) {
                $html .= "    <td class=values valign=top>failed unit ".
                         "$promoted</td>\n";
            } else {
                $html .= "    <td class=values valign=top>$states[$status]".
                         " $promoted</td>\n";
            }

            $html .= "    <td class=values valign=top>\n";

            foreach ($teststeps as $tests) {
                $html .= $tests;
            }

            # &user=1&access=login

            $html .= "    </td>\n".
                     "    <td class=values valign=top>$date</td>\n".
                     "    <td class=values valign=top>$duration</td>\n".
                     "    <td class=values valign=top>\n".
                     "\t<a href=/adminbroker.php?cmd=promote&server=".
                     "$buildserver&jobname=$jobname&jobno=$jobno&sleep=".
                     "1 onmouseover=\"Tip('promote build $jobno')\">".
                     "<img src=/cabieicons/folder_go.png ".
                     "alt=\"promote\"></a>&nbsp;\n".
                     "\t<a href=/$buildserver/$jobname/$jobno><img src=".
                     "/cabieicons/".
                     "report.png alt=\"view logs\" ".
                     "onmouseover=\"Tip('view build $jobno logs')\">".
                     "</a>&nbsp;\n".
                     "$comment".
                     "&nbsp;\n".
                     "\t<a href=/userbroker.php?cmd=teststatus&server=".
                     "$buildserver&jobname=$jobname&jobno=$jobno><img src=/".
                     "cabieicons/cog.png alt=\"view tests\" ".
                     "onmouseover=\"Tip('tests executed for build $jobno')\">".
                     "</a>&nbsp;\n".
                     "    </td>\n".
                     "</tr>\n";

        }
 
        return $html;

    }

    function _checkPromotion($SQL, $title, $job) {

        $sqlarray = $SQL->sql_query("select title from promotion where ".
                                    "title=\"$title\" and job=\"$job\"",
                                    ";");

        if (!count($sqlarray)) {
            return "";
        } else {
            return "/ promoted";
        }

    }
        
    function _formatTestStates($SQL, $title, $job, $server) {

        $a = 0;

        $return   = array();
        $testhash = array();

        $tests = array("notest", "runningtest", "passedtest", "failedtest");

        $platform = array("solaris", "linux", "hpux", "windows");

        $sqlarray = $SQL->sql_query("select title from teststate where ".
                                    "jobname=\"$title\" and job=\"$job\" ".
                                    "order by title", ";");

        if (!count($sqlarray)) {
            return array("        not run\n");
        }

        foreach ($sqlarray as $name) {

            list($testname) = explode(";", $name);

            if ((strpos($testname, "sol")) !== false) {
                $plat = 0;
            }
            if ((strpos($testname, "lin")) !== false) {
                $plat = 1;
            }
            if ((strpos($testname, "hpux")) !== false) {
                $plat = 2;
            }
            if ((strpos($testname, "win")) !== false){
                $plat = 3;
            }

            $testhash["$name"] = $plat;

        }

        $numrecs = count($testhash);

        $return[$a++] = "    <table width=100%>\n";

        $return[$a++] = "    <tr><td class=testname colspan=$numrecs>Executed ".
                        "tests</td></tr>\n";

        $hashcount = 0;

        foreach ($testhash as $key => $value) {

            $sqlarray = $SQL->sql_query("select step,state from teststate ".
                                        "where jobname=\"$title\" and ".
                                        "job=\"$job\" and title=\"$key\" ".
                                        "order by start", ";");
    
            $reccount = 0;
           
            $fulltest[$hashcount][$reccount++] = "\t    <td class=platform>".
                                                 "<a href=/$server/".
                                                 "$title/$job/testreport/".
                                                 "$platform[$value]>".
                                                 "$platform[$value]</a>".
                                                 "</td>";

            foreach ($sqlarray as $record) {
    
                list($step, $state) = explode(";", $record);
    
                if ($state) {
                    $fulltest[$hashcount][$reccount++] = "\t    <td class=".
                                                    "$tests[$state]>".
                                                    "$step</td>";
                }
    
            }
            $hashcount++;

        }

        $cols = 0;
        $recs = 0;

        $numrecs = count($fulltest);

        $bRun = 1;

        if ($numrecs) {
            $allcols = count($fulltest[0]);
            while ($recs < $allcols) {
                $return[$a++] = "\t<tr>\n";
                while ($cols < $numrecs) {
                    $return[$a++] = $fulltest[$cols][$recs]."\n";
                    $cols++;
                }
                $return[$a++] = "\t</tr>\n";
                $cols=0;
                $recs++;
            }
        }
        
        $return[$a++] = "    </table>\n";

        return $return;

    }

    #
    # initialize sajax, and export function to be used via ajax
    #
    sajax_init();
    sajax_export("ajax_jobinfo");
    sajax_handle_client_request();

?>
